package com.eras.shoes.db;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class MacBlacklistDbHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "mac_blacklist.db";
    private static final int DB_VERSION = 1;
    public static final String TABLE_NAME = "blacklisted_mac";
    public static final String COL_MAC = "mac_address";

    public MacBlacklistDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COL_MAC + " TEXT UNIQUE NOT NULL)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public boolean addMac(String mac) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_MAC, mac);
        return db.insertWithOnConflict(TABLE_NAME, null, values,
                SQLiteDatabase.CONFLICT_IGNORE) != -1;
    }

    public boolean removeMac(String mac) {
        SQLiteDatabase db = getWritableDatabase();
        return db.delete(TABLE_NAME, COL_MAC + "=?",
                new String[]{mac}) > 0;
    }

    public List<String> getAllMacs() {
        List<String> macs = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, new String[]{COL_MAC},
                null, null, null, null, null);
        while (cursor.moveToNext()) {
            macs.add(cursor.getString(0));
        }
        cursor.close();
        return macs;
    }
}

